package com.colorful.insuranceproject.fragment;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.bean.PersonInfoBean;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.dialog.QuitAlertDialog;
import com.colorful.insuranceproject.microDrug.TransactionRecordActivity;
import com.colorful.insuranceproject.mine.BankCardListActivity;
import com.colorful.insuranceproject.mine.BindBankActivity;
import com.colorful.insuranceproject.mine.CompanyInfoActivity;
import com.colorful.insuranceproject.mine.DeliveryActivity;
import com.colorful.insuranceproject.mine.ModifyLoginPwdActivity;
import com.colorful.insuranceproject.mine.QuotaInfoActivity;
import com.colorful.insuranceproject.mine.UserInfoActivity;
import com.colorful.insuranceproject.request.MineRequest;
import com.lovely3x.common.fragments.BaseCommonFragment;
import com.lovely3x.common.managements.user.UserManager;
import com.lovely3x.common.utils.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-bob on 2017/5/4.
 * <p>
 * 我的Fragment
 */
public class MineFragment extends BaseCommonFragment {

    private static final int GET_MINE_FRAGMENT_PAGE_DATA = 0X1;

    private MineRequest mineRequest;

    private PersonInfoBean pb;

    /**
     * 实名认证
     */
    @Bind(R.id.tv_authentication)
    TextView tvAuthentication;

    /**
     * 绑定手机号
     */
    @Bind(R.id.tv_bind_phone)
    TextView tvBindPhone;

    /**
     * 绑定收货地址
     */
    @Bind(R.id.tv_bind_delivery)
    TextView tvBindDelivery;

    /**
     * 银行卡绑定状态
     */
    @Bind(R.id.tv_bind_bank)
    TextView tvBindBank;

//    750 X 180
//    750 X 300
    /**
     * 银行卡添加状态
     */
    @Bind(R.id.tv_add_bank_card)
    TextView addBankCard;

    @Override
    protected int getContentView() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this, mRootView);
    }

    @Override
    protected void onViewInitialized() {
        mineRequest = new MineRequest(getHandler());
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case GET_MINE_FRAGMENT_PAGE_DATA:
                if (response.isSuccessful) {
                    pb = (PersonInfoBean) response.obj;
                    //实名认证
                    tvAuthentication.setText(pb.getIsAuth() == 1 ? getString(R.string.isAuth) : getString(R.string.Immediate_authentication));

                    //绑定手机号
                    tvBindPhone.setText(!pb.getUserPhone().equals("") ? getString(R.string.binded) : getString(R.string.not_bind));

                    //绑定收获地址
                    tvBindDelivery.setText(pb.getHasUserAddress() == 1 ? getString(R.string.binded) : getString(R.string.not_bind));

                    // 银行卡绑定状态
                    tvBindBank.setText(pb.getBindBank().getBankCardAuth().equals("1") ? getString(R.string.binded) : getString(R.string.not_bind));

                    //  银行卡添加
                    addBankCard.setText(pb.getHasBankCard() == 1 ? getString(R.string.binded) : getString(R.string.add_bank_card));
                } else {
                    mActivity.showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @Override
    protected void restoreInstanceOnCreateBefore(Bundle savedInstanceState) {

    }

    @Override
    protected void restoreInstanceOnCreateAfter(Bundle savedInstanceState) {

    }

    @OnClick({R.id.tv_exit, R.id.rl_company_info, R.id.rl_user_info, R.id.rl_quota_information,
            R.id.rl_immediate_authentication, R.id.rl_bind_phone, R.id.rl_bind_bank, R.id.rl_bank_card_manager,
            R.id.rl_modify_pwd, R.id.rl_delivery_address, R.id.rl_business_record, R.id.rl_modify_pay_pwd})
    void OnClicked(View v) {
        switch (v.getId()) {
            // 公司信息
            case R.id.rl_company_info:
                if (pb != null)
                    mActivity.launchActivityNeedLogin(CompanyInfoActivity.class, CompanyInfoActivity.EXTRA_COMPANY_INFO, pb.getCompanyInfo());
                break;
            // 个人信息
            case R.id.rl_user_info:
                if (pb != null)
                    mActivity.launchActivityNeedLogin(UserInfoActivity.class, UserInfoActivity.EXTRA_USER_INFO, pb);
                break;
            // 账户信息
            case R.id.rl_quota_information:
                if (pb != null)
                    mActivity.launchActivityNeedLogin(QuotaInfoActivity.class, QuotaInfoActivity.EXTRA_QUOTA_INFO, pb.getMoneyInfo());
                break;
            //  实名认证
            case R.id.rl_immediate_authentication:
                if (pb != null)
                    mActivity.launchActivityNeedLogin(UserInfoActivity.class, UserInfoActivity.EXTRA_USER_INFO, pb);
                break;
            //  绑定手机
            case R.id.rl_bind_phone:
                if (pb != null)
                    mActivity.launchActivityNeedLogin(UserInfoActivity.class, UserInfoActivity.EXTRA_USER_INFO, pb, UserInfoActivity.EXTRA_TYPE, 1);
                break;
            //  绑定银行卡
            case R.id.rl_bind_bank:
                if (pb != null)
                    mActivity.launchActivityNeedLogin(BindBankActivity.class, BindBankActivity.EXTRA_BANK_INFO, pb.getBindBank());
                break;
            //  银行卡管理
            case R.id.rl_bank_card_manager:
                mActivity.launchActivityNeedLogin(BankCardListActivity.class);
                break;
            //  支付密码
            case R.id.rl_modify_pay_pwd:

                break;
            //  修改密码
            case R.id.rl_modify_pwd:
                mActivity.launchActivityNeedLogin(ModifyLoginPwdActivity.class);
                break;
            // 收货地址
            case R.id.rl_delivery_address:
                mActivity.launchActivityNeedLogin(DeliveryActivity.class);
                break;
            //  交易记录
            case R.id.rl_business_record:
                mActivity.launchActivityNeedLogin(TransactionRecordActivity.class);
                break;
            //  退出登录
            case R.id.tv_exit:
                showPop();
                break;
        }
    }

    @Override
    public void onUserHintVisibleChanged(boolean isVisibleToUser, boolean isFirstVisible) {
        super.onUserHintVisibleChanged(isVisibleToUser, isFirstVisible);
        if (isVisibleToUser) {
            if (!UserManager.getInstance().isSigned()) {
                mActivity.launchLoginActivity();
            } else {
                mineRequest.getUserInfoUrl(GET_MINE_FRAGMENT_PAGE_DATA);
            }
        }
    }

    /**
     * 加载退出窗口
     */
    private void showPop() {
        if (UserManager.getInstance().isSigned()) {
            final QuitAlertDialog quitAlertDialog = QuitAlertDialog.newInstance();
            quitAlertDialog.setCallBack(new QuitAlertDialog.CallBack() {
                @Override
                public boolean onSureClicked() {
                    UserManager.getInstance().logout();
                    mActivity.launchLoginActivity();
                    return false;
                }

                @Override
                public boolean onCancelClicked() {
                    return false;
                }
            });
            quitAlertDialog.show(mActivity.getSupportFragmentManager(), QuitAlertDialog.class.getName());
        } else {
            mActivity.launchLoginActivity();
        }
    }
}
